package org.elanfox.cloud.common.web.config;

import org.elanfox.cloud.common.web.interceptor.RestAuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author allan
 * @date 2019-08-16
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    @ConditionalOnProperty(
            value = {"fox.cloud.service.enableToken"},
            havingValue = "true",
            matchIfMissing = true
    )
    public RestAuthInterceptor authInterceptor() {
        return new RestAuthInterceptor();
    }

    @Autowired(required = false)
    private RestAuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (authInterceptor != null) {
            registry.addInterceptor(new RestAuthInterceptor());
        }
    }
}
